package com.justgame.project.generator.schema;

import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Objects;

public class Field {
    private final String name;
    private final String comment;
    private final DbColumnType type;
    private BigDecimal length;
    private boolean isEnum;
    private boolean isEncrypt;

    private Field(String name, String comment, DbColumnType type) {
        this.name = name;
        this.comment = comment;
        this.type = type;
    }

    public Field setEncrypt(boolean encrypt) {
        isEncrypt = encrypt;
        return this;
    }

    public boolean isEncrypt() {
        return isEncrypt;
    }

    public boolean isEnum() {
        return isEnum;
    }

    public String getName() {
        return name;
    }

    public DbColumnType getType() {
        return type;
    }

    public String getComment() {
        return comment;
    }

    public BigDecimal getLength() {
        return length;
    }

    private Field length(BigDecimal len){
        Assert.isTrue(Objects.isNull(this.length) && Objects.nonNull(this.type),
                String.format("字段类型： %s 不允许自定义修改字段长度.",this.type.getType()));
        this.length = len.compareTo(BigDecimal.ZERO) == 0 ? null : len;
        return this;
    }

    public Field setEnum(boolean anEnum) {
        isEnum = anEnum;
        return this;
    }

    public static Field string(String name, String comment){
        return new Field(name,comment,DbColumnType.STRING).length(BigDecimal.valueOf(255));
    }

    public static Field url(String name,String comment){
        return new Field(name,comment,DbColumnType.STRING).length(BigDecimal.valueOf(255));
    }

    public static Field integer(String name, String comment) {
        return integer(name,comment,BigDecimal.valueOf(9));
    }

    public static Field integer(String name, String comment, BigDecimal length){
        return new Field(name,comment,DbColumnType.INTEGER).length(length);
    }

    public static Field decimal(String name, String comment){
        return new Field(name,comment,DbColumnType.BIG_DECIMAL);
    }

    public static Field bool(String name, String comment){
        return new Field(name,comment,DbColumnType.BOOLEAN).length(BigDecimal.ZERO);
    }

    public static Field text(String name, String comment){
        return new Field(name,comment,DbColumnType.CLOB).length(BigDecimal.ZERO);
    }

    public static Field key(String name, String comment){
        return new Field(name,comment,DbColumnType.LONG).length(BigDecimal.ZERO);
    }

    public static Field dateTime(String name, String comment){
        return new Field(name,comment,DbColumnType.LOCAL_DATE_TIME).length(BigDecimal.ZERO);
    }

    public static Field date(String name, String comment){
        return new Field(name,comment,DbColumnType.LOCAL_DATE).length(BigDecimal.ZERO);
    }

    public static Field type(String name, String comment){
        return new Field(name,comment,DbColumnType.STRING).length(BigDecimal.valueOf(50)).setEnum(true);
    }

    public static Field bigint(String name, String comment){
        return new Field(name,comment,DbColumnType.LONG).length(BigDecimal.valueOf(11));
    }

    public static Field encrypt(String name,String comment){
        return new Field(name,comment,DbColumnType.STRING)
                .length(BigDecimal.valueOf(255))
                .setEncrypt(true)
                .setEnum(false);
    }
}
